package com.ruoyi.web.controller.erp.transport;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.transport.domian.TransportUnitType;
import com.ruoyi.erp.transport.service.TransportUnitTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * yun.wang
 * 运输单位类型
 */
@Api("运输单位类型")
@Controller
@RequestMapping("/transport")
public class TransportUnitTypeController extends BaseController {
    @Autowired
    TransportUnitTypeService transportUnitTypeService;
    private String prefix = "erp/transport/list";

    @RequiresPermissions("transport:list:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/transport";
    }

    @ApiOperation("运输单位类型列表")
    @RequiresPermissions("transport:list:list")
    @RequestMapping(value = "list",method = {RequestMethod.POST})
    @ResponseBody
    public TableDataInfo selectIsoUnitAll(
            @RequestParam(value = "weightCapacity",required = false) String weightCapacity,
            @RequestParam(value = "volumeCapacity",required = false) String volumeCapacity,
            @RequestParam(value = "transportUnitTypeId",required = false) String transportUnitTypeId,
            @RequestParam(value = "description",required = false) String description,
            @RequestParam(value = "uomForWeight",required = false) String uomForWeight,
            @RequestParam(value = "uomForVolume",required = false) String uomForVolume
    ){
        startPage();
        List<TransportUnitType> json =transportUnitTypeService.selectTransportUnitTypeAll(
                weightCapacity,
                volumeCapacity,
                transportUnitTypeId,
                description,
                uomForWeight,
                uomForVolume);
        return getDataTable(json);
    }


    /**
     * 新增运输单位类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存运输单位类型
     */
    @RequiresPermissions("transport:list:add")
    @Log(title = "运输单位类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TransportUnitType type)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(transportUnitTypeService.checkTransportCodeUnique(type)))
        {
            return error("新增运输单位类型'" + type.getTransportUnitTypeId() + "'失败，运输单位类型已存在");
        }
        type.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(transportUnitTypeService.insertTransport(type));
    }


    /**
     * 修改运输单位类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String transportUnitTypeId, ModelMap mmap)
    {
        mmap.put("transport", transportUnitTypeService.selectTransportCode(transportUnitTypeId));
        return prefix + "/edit";
    }


    /**
     * 修改保存运输单位类型
     */
    @RequiresPermissions("transport:list:edit")
    @Log(title = "运输单位类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated TransportUnitType type)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(transportUnitTypeService.checkTransportCodeUnique(type)))
        {
            return error("修改运输单位类型'" + type.getTransportUnitTypeId() + "'失败，运输单位类型已存在");
        }
        type.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(transportUnitTypeService.updateTransport(type));
    }
    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TransportUnitType> util = new ExcelUtil<TransportUnitType>(TransportUnitType.class);
        return util.importTemplateExcel("运输单位类型数据");
    }

    @Log(title = "运输单位类型", businessType = BusinessType.IMPORT)
    @RequiresPermissions("transport:list:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TransportUnitType> util = new ExcelUtil<TransportUnitType>(TransportUnitType.class);
        List<TransportUnitType> transportList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = transportUnitTypeService.importTransport(transportList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @Log(title = "运输单位类型", businessType = BusinessType.EXPORT)
    @RequiresPermissions("transport:list:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String ids)
    {
        List<TransportUnitType> list = transportUnitTypeService.selectTransportUnitList(ids);
        ExcelUtil<TransportUnitType> util = new ExcelUtil<TransportUnitType>(TransportUnitType.class);
        return util.exportExcel(list, "运输单位类型");
    }

    @RequiresPermissions("transport:list:remove")
    @Log(title = "运输单位类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(transportUnitTypeService.deleteTransportByIds(ids));
    }

}
